package ddd6.work001;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Call {
    static Random random = new Random();
    public static void main(String[] args) {
        System.out.println("-----随机点名器-----");
        ArrayList<Student> students = new ArrayList<>();
        addStudents(students);
        printStudents(students);
        randomCall(students);
    }

    public static void addStudents(ArrayList<Student> students) {
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("\n正在录入第 " + (i + 1) + " 个学生信息：");

            String name = getValidName(scanner, i);
            String sex = getValidSex(scanner, i);
            int age = getValidAge(scanner, i);

            students.add(new Student(i, name, sex, age));
        }
    }

    private static String getValidName(Scanner scanner, int index) {
        while (true) {
            System.out.print("存储第" + (index + 1) + "个学生的名称：");
            String input = scanner.nextLine().trim();
            if (input.isEmpty()) {
                System.out.println("错误：姓名不能为空！");
            } else if (input.matches(".*\\d.*")) {
                System.out.println("错误：姓名不能包含数字！");
            } else {
                return input;
            }
        }
    }

    private static String getValidSex(Scanner scanner, int index) {
        while (true) {
            System.out.print("存储第" + (index + 1) + "个学生的性别（男/女）：");
            String input = scanner.nextLine().trim();
            if (input.equals("男") || input.equals("女")) {
                return input;
            }
            System.out.println("错误：性别只能输入'男'或'女'！");
        }
    }

    private static int getValidAge(Scanner scanner, int index) {
        while (true) {
            System.out.print("存储第" + (index + 1) + "个学生的年龄：");
            try {
                int age = Integer.parseInt(scanner.nextLine());
                if (age >= 18 && age <= 40) {
                    return age;
                }
                System.out.println("错误：年龄必须在18-40之间！");
            } catch (NumberFormatException e) {
                System.out.println("错误：请输入有效的数字年龄！");
            }
        }
    }

    public static void printStudents(ArrayList<Student> students) {
        System.out.println("\n当前学生列表：");
        for (int i = 0; i < students.size(); i++) {
            Student s = students.get(i);
            System.out.println("编号：" + s.getId() +
                    " 姓名：" + s.getName() +
                    " 性别：" + s.getSex() +
                    " 年龄：" + s.getAge());
        }
    }

    public static void randomCall(ArrayList<Student> students) {
        if (students.isEmpty()) {
            System.out.println("\n没有学生可点名，请先添加学生！");
            return;
        }

        int index = random.nextInt(students.size());
        Student luckyOne = students.get(index);
        System.out.println("\n===== 随机点名结果 =====");
        System.out.println("被点到的学生是：" + luckyOne.getName());
        System.out.println("详细信息：");
        System.out.println("ID: " + luckyOne.getId());
        System.out.println("性别: " + luckyOne.getSex());
        System.out.println("年龄: " + luckyOne.getAge());
    }
}